﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SyncList.UserInterface.ValueObject;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using SyncList.Business.ValueObject;

namespace SyncList.UserInterface.Helpers
{


    /// <summary>
    /// Aide pour les icônes
    /// </summary>
    public static class IconHelper
    {


        /// <summary>
        /// Renvoyer l'icône appropriée
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static string GetIconName(ItemBase item)
        {
            string result = "unknown.png";
            DirectoryContent directory = item as DirectoryContent;
            if (directory != null)
            {
                result = "folder.png";
              
                switch (directory.DirectoryType)
                {
                    case DirectoryTypeEnum.Back:
                        result = "back.png";
                        break;

                    case Business.ValueObject.DirectoryTypeEnum.Root:
                        result = "base.png";
                        break;

                    case Business.ValueObject.DirectoryTypeEnum.User:
                        result = "user.png";
                        break;

                    case Business.ValueObject.DirectoryTypeEnum.HardDisk:
                        result = "harddisk.png";
                        break;

                }
                

                
            }

            FileItem file = item as FileItem;
            if (file != null)
            {

                string[] video = new string[] { ".avi", ".divx", ".mkv", ".mpg", ".mpeg", ".wmv", ".asf", ".flv", ".mov", ".m2v", ".mp4", ".rm", ".swf", ".ts", ".vob" };
                string[] photo = new string[] { ".jpg", ".jpeg", ".cr2", ".gif", ".png", ".tif", ".tiff", ".raw", ".bmp" };
                string[] music = new string[] { ".mp3", ".mpc", ".ogg", ".ogm", ".wma" };                               
                string[] word = new string[] { ".doc", ".docx", ".dotx", ".docm", ".stw", ".sxw" };                               
                string[] excel = new string[] { ".xls", ".xlsx", ".xlsm", ".xltx", ".xltm", ".xlsb", ".xlam", ".sxm" };
                string[] presentation = new string[] { ".ppt", ".pot", ".pps", ".sxi", ".sti", ".pptx", ".pptm", ".potx", ".potm", ".ppam", ".ppsx", ".ppsm", ".sldx", ".sdlm", ".thmx" };
                string[] diskimage = new string[] { ".iso", ".mds", ".b5t", ".b6t", ".bwt", ".ccd", ".bin", ".cue", ".isz", ".nrg", ".pdi", ".cdi" };
                string[] cmd = new string[] { ".cmd", ".bat", ".msi", ".vbs", ".vbe", ".vb", ".com", ".pif", ".wsc", ".wsf", ".msp" };
                string[] exe = new string[] { ".exe", ".scr" };
                string[] dll = new string[] { ".dll", ".ocx" };
                string[] xsl = new string[] { ".xsl", ".xslt" };
                string[] xml = new string[] { ".xsl", ".xslt" };
                string[] cs = new string[] { ".cs" };
                string[] html = new string[] { ".htm", ".html" };
                

                List<string> archive = new List<string>();
                archive.AddRange(new string[] { ".rar", ".zip", ".7z", ".ace", ".cbr", ".cab" });
                for (int i = 1; i < 99; i++)
                    archive.Add("r" + i.ToString("00"));

                string[] equivalent = new string[] { ".psd", ".pdf", ".psd", ".raw", ".reg", ".nfo", ".bak", ".log", ".xaml", ".par2", ".nzb", ".sav" };

                string ext = file.Extension.ToLower(); // GetExtension(item).ToLower();
                if (video.Contains(ext))
                    result = "video.png";
                else if (photo.Contains(ext))
                    result = "photo.png";
                else if (music.Contains(ext))
                    result = "music.png";
                else if (archive.Contains(ext))
                    result = "archive.png";
                else if (word.Contains(ext))
                    result = "word.png";
                else if (excel.Contains(ext))
                    result = "excel.png";
                else if (presentation.Contains(ext))
                    result = "pps.png";                    
                else if (diskimage.Contains(ext))
                    result = "diskimage.png";
                else if (cmd.Contains(ext))
                    result = "cmd.png";
                else if (exe.Contains(ext))
                    result = "exe.png";
                else if (dll.Contains(ext))
                    result = "dll.png";
                else if (xml.Contains(ext))
                    result = "xml.png";
                else if (xsl.Contains(ext))
                    result = "xsl.png";
                else if (cs.Contains(ext))
                    result = "cs.png";
                else if (html.Contains(ext))
                    result = "html.png";
                else if (equivalent.Contains(ext))  // Equivalence
                    result = ext.Substring(1) + ".png";

            }

            return (result);
        }



        /// <summary>
        /// Renvoie uniquement l'extension
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetExtension(string name)
        {
            string extension = "";
            int extPos = name.LastIndexOf('.');
            if(extPos >= 0)
                extension = name.Substring(extPos);
            return (extension);
        }


        /// <summary>
        /// Renvoyer un ImageBrush
        /// </summary>
        /// <param name="item">Elément dont il faut trouver l'icône</param>
        /// <returns></returns>
        public static ImageBrush GetIcon(ItemBase item)
        {
            ImageBrush result = null;
            string iconName = GetIconName(item);
            if(!String.IsNullOrWhiteSpace(iconName))
                result = GetImageBrush(iconName);
            return (result);
        }


        /// <summary>
        /// Renvoyer un ImageBrush
        /// </summary>
        /// <param name="imageName"></param>
        /// <returns></returns>
        public static ImageBrush GetImageBrush(string imageName)
        {
            ImageBrush imgBrush = new ImageBrush(new BitmapImage(new Uri("Images/" + imageName, UriKind.Relative)));
            imgBrush.Stretch = Stretch.Uniform;
            return (imgBrush);
        }

    }
}
